package com.yourcom.proname.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yourcom.proname.repository.entity.bizDb.UserAccount;
import com.yourcom.proname.repository.entity.testDb.MainUser;
import com.yourcom.proname.repository.service.IMainUserService;
import com.yourcom.proname.repository.service.IUserAccountService;
import com.yourcom.proname.vo.CommonResVo;
import com.yourcom.proname.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;


@RestController
public class UserController {
    @Resource
    IMainUserService mainUserService;

    @Resource
    IUserAccountService userAccountService;

    @PostMapping("user/register") //定义post请求
    public CommonResVo<Boolean> userRegister(@RequestBody MainUser mainUser) throws Exception {
        //验证用户名是否已存在
        MainUser oldUser = mainUserService.getOne(new LambdaQueryWrapper<MainUser>().eq(MainUser::getUserName, mainUser.getUserName()));
        if (oldUser != null) {
            throw new Exception("用户已存在");
        }
        //用户密码存表时，要做加密处理，可以直接使用spring提供的DigestUtils工具类生成32位MD5字符串
        String password = DigestUtils.md5DigestAsHex(mainUser.getPassword().getBytes());
        mainUser.setPassword(password);
        mainUserService.save(mainUser);
        return CommonResVo.success(true);
    }

    @PostMapping("user/login")
    public CommonResVo<Boolean> userLogin(@RequestBody HashMap<String, String> loginInfo, HttpSession session) throws Exception {
        String userName = loginInfo.get("userName");
        String password = loginInfo.get("password");
        //通过用户名获取用户信息
        MainUser mainUser = mainUserService.getOne(new LambdaQueryWrapper<MainUser>().eq(MainUser::getUserName, userName));
        if (mainUser == null) {
            throw new Exception("用户不存在");
        }

        //对比MD5密码
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!mainUser.getPassword().equals(md5Password)) {
            throw new Exception("账号名或密码错误");
        }
        //将userId存入session中
        session.setAttribute("userId", mainUser.getUserId());
        return CommonResVo.success(true);
    }

    /**
     * 根据id获取用户
     *
     * @return
     */
    @GetMapping("user/info")
    public CommonResVo<UserVo> getUserById(HttpSession session) throws Exception {
        MainUser mainUser = mainUserService.getById((Integer) session.getAttribute("userId"));
        if (mainUser == null) {
            throw new Exception("用户不存在");
        }
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(mainUser, userVo);
        return CommonResVo.success(userVo);
    }

    /**
     * 根据id获取用户账户信息
     *
     * @return
     */
    @GetMapping("user/account")
    public CommonResVo<UserAccount> getUserAccount(Integer userId) throws Exception {
        MainUser mainUser = mainUserService.getById(userId);
        if (mainUser == null) {
            throw new Exception("用户不存在");
        }
        UserAccount userAccount = userAccountService.getOne(new LambdaQueryWrapper<UserAccount>().eq(UserAccount::getUserId, mainUser.getUserId()));
        return CommonResVo.success(userAccount);
    }
}
